package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api("菜品管理")
public class DishController {
    @Autowired
    private DishService dishService;
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO queryDTO){
        PageResult page = dishService.page(queryDTO);
        return Result.success(page);
    }

    @PostMapping()
    @ApiOperation("添加菜品")
    @CacheEvict(cacheNames = "dishCache", allEntries = true)
    public Result dish(@RequestBody DishDTO dishDTO){
        dishService.dish(dishDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取菜品信息")
    @Cacheable(cacheNames = "dishCache",key = "#id")
    public Result<DishVO> getDishByID(@PathVariable long id){
        DishVO dishVO=dishService.getDishByID(id);
        return Result.success(dishVO);
    }

    @PutMapping()
    @ApiOperation("修改菜品信息")
    @CacheEvict(cacheNames = "dishCache", allEntries = true)
    public Result modify(@RequestBody DishDTO dishDTO){
        dishService.modify(dishDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("菜品停售")
    @CacheEvict(cacheNames = "dishCache", allEntries = true)
    public Result stopOrStart(@PathVariable long status,long id){
        dishService.stopOrStart(status,id);
        return Result.success();
    }

    @DeleteMapping()
    @ApiOperation("批量删除")
    @CacheEvict(cacheNames = "dishCache", allEntries = true)
    public Result DeleteDishes(@RequestParam List<Long> ids){
        log.info("删除菜品的id:{}",ids);
       dishService.deleteDishes(ids);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("菜品分类查询")
    public Result<List<Dish>> list(String categoryId){
        List<Dish> dish = dishService.list(Long.valueOf(categoryId));
        return Result.success(dish);
    }
}
